﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;


namespace Chaos.commands
{
    public class hostgame : ICommand
    {
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        } 
        
        public virtual bool CanExecute(object parameter)
        {
            network.gameserver s = System.Windows.Application.Current.FindResource("game_server") as network.gameserver;
            if (s != null)
            {
                return !s.Running;
            }
            return false;
        }

        public virtual void Execute(object parameter)
        {
            network.gameserver s = System.Windows.Application.Current.FindResource("game_server") as network.gameserver;
            if (s != null)
            {
                s.start();  
            }

            network.detectionserver d = System.Windows.Application.Current.FindResource("detection_server") as network.detectionserver;
            if (d != null)
            {
                d.start(network.detectionserver.Port);
            }

            network.gameclient c = System.Windows.Application.Current.FindResource("game_client") as network.gameclient;
            if (c != null)
            {
                c.open("127.0.0.1", network.gameserver.Port);
            }

            model.appinfo a = System.Windows.Application.Current.FindResource("model_appinfo") as model.appinfo;
            if (a != null)
            {
                a.start();
            }
          
            view.manager m = System.Windows.Application.Current.FindResource("view_manager") as view.manager;
            if (m != null)
            {
                MainWindow w = m.Controls["main_window"] as MainWindow;
                if (w != null)
                {
                    w.ScreenGrid_show(w.LobbyScreen);
                }
            }
        }
    }
}
